package leetcode.medium.limit50;

/**
 * 整数转罗马数字
 *
 * @author yaoyao1.liu@zhaopin.com.cn
 * @since 2022-06-30 10:27
 **/
public class No12 {

    public static void main(String[] args) {
        No12 no12 = new No12();
        String intToRoman = no12.intToRoman(1994);
        System.out.println(intToRoman);

    }

    public String intToRoman(int num) {
        int k = num / 1000;
        int a = num - (k * 1000);
        int h = a / 100;
        int b = a - (h * 100);
        int s = b / 10;
        int g = b % 10;

        StringBuilder sb = new StringBuilder();
        if (k > 0) {
            for (int i = 0; i < k; i++) {
                sb.append("M");
            }
        }
        if (h > 0) {
            deal(h, sb, "C", "D", "M");
        }
        if (s > 0) {
            deal(s, sb, "X", "L", "C");
        }
        if (g > 0) {
            deal(g, sb, "I", "V", "X");
        }
        return sb.toString();
    }

    public void deal(int h, StringBuilder sb, String a, String b, String c) {
        if (h == 4) {
            sb.append(a).append(b);
        } else if (h == 9) {
            sb.append(a).append(c);
        } else if (h < 5) {
            for (int i = 0; i < h; i++) {
                sb.append(a);
            }
        } else if (h == 5) {
            sb.append(b);
        } else {
            sb.append(b);
            for (int i = 0; i < (h - 5); i++) {
                sb.append(a);
            }
        }
    }
}
